package cn.snailhd.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

import javax.swing.plaf.synth.SynthScrollBarUI;

public class HttpRequest {
	protected Logger log=Logger.getLogger(this.getClass().getName());
	private HttpURLConnection connection;
	
	public HttpURLConnection getConnection() {
		return connection;
	}
	public HttpRequest setConnection(HttpURLConnection connection) {
		this.connection = connection;
		return this;
	}

	public HttpRequest(String httpUrl) throws IOException{
		URL url;

		url = new URL(httpUrl);
		connection = (HttpURLConnection) url.openConnection();
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("upgrade-insecure-requests", "1");
		connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
		connection.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		connection.setRequestProperty("accept-encoding", "deflate, sdch, br");
		connection.setRequestProperty("accept-language", "zh-CN,zh;q=0.8");
		connection.setRequestMethod("GET");
		connection.connect();
	
	}
	
	public String getContent(){
		StringBuilder sb = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		BufferedInputStream bis = null;
		String encode = null;
		try {
			bis = new BufferedInputStream(connection.getInputStream());
			int len;
			byte[] buff  = new byte[1024*128];
			
			while((len=bis.read(buff))>-1){
				if(encode == null){
					String temp = new String(buff, 0, len);
					sb2.append(temp);
					if(temp.indexOf("charset")!=-1){
						String ss = temp.substring(temp.indexOf("charset="));
						encode = ss.substring(8, ss.indexOf("\""));
						//System.out.println(encode);
						sb.append(new String(buff,0,len,encode));
					}
				}else {
					sb.append(new String(buff,0,len,encode));
				}	
			}
	
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			IoUtils.close(bis);
		}
		if(encode == null){
			return sb2.toString();
		}
		return sb.toString();
	
	}
	
	public String download(String folder,String filename){
		
		String file = null;
		File _file=new File(folder+filename);;
		if(!"".equals(folder)){
			if(!_file.getParentFile().exists()){
				_file.getParentFile().mkdirs();
			}
		}

		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		try {
			bis =new BufferedInputStream( connection.getInputStream());
			bos = new BufferedOutputStream(new FileOutputStream(_file));
			byte[] buff = new byte[1024*1024];//1m
			int len;
			long size = 0;
			while((len=bis.read(buff))!=-1){
				size+=len;
				bos.write(buff, 0, len);
				bos.flush();
				Thread.sleep(50);	
			}
			file=_file.getAbsolutePath();
		} catch (Exception e) {
			e.printStackTrace();
			file=null;
		} finally {
			IoUtils.close(bos);
			IoUtils.close(bis);
		}
		
		return file;
	}
	
	
	public static void main(String[] args) throws IOException {
		/*long begin = System.currentTimeMillis();
		HttpRequest request = new HttpRequest("http://101.110.118.23/big1.wy119.com/adobe_photoshop_cc2015.zip");
		//String html = request.getContent();
		request.download("D:\\", "adobe_photoshop_cc2015.zip");
		long size = request.getConnection().getContentLengthLong();
		long end = System.currentTimeMillis();
		System.out.println(end-begin+"ms,"+(end-begin)/1000+"s");
		System.out.println("size:"+size);*/
		
		
		HttpRequest request = new HttpRequest("http://firozstar.tripod.com/_darksiderg.pdf");
		
		request.download("D:\\", "darksiderg.pdf");
			
	}
	
	
	
	
}
